<?php
/**
 * 商品属值
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class GoodsAttribute extends Model
{
    //set talbename
    protected $table = 'goods_attribute';
    //set primary key
    protected $pk = 'attr_id';

    /**
     * @desc 增加商品属性值
     * @param $goods_id
     * @param $attr_value
     * @param $attr_price
     * @return false|int
     */
    public function addGoodsAttribute($param)
    {
        $this->allowField(true)->save($param);
        return $this->data[$this->pk];
    }

    public function updateGoodsAttribute($attr_id, $param)
    {
        $conds = array(
            'attr_id' => $attr_id,
        );
        $field = array();
        if (isset($param['cat_id'])) {
            $field['cat_id'] = $param['cat_id'];
        }

        if (isset($param['attr_name'])) {
            $field['attr_name'] = $param['attr_name'];
        }

        if (isset($param['attr_values'])) {
            $field['attr_values'] = $param['attr_values'];
        }

        if (isset($param['order'])) {
            $field['order'] = $param['order'];
        }

        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    public function deleteGoodsAttribute($attr_id)
    {
        $conds = array(
            'attr_id' => $attr_id,
        );
        return Db::table($this->table)->where($conds)->delete();
    }

    /**
     * @desc 获取商品属性值
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGoodsAttribute()
    {
        $field = '*';
        return Db::table($this->table)->field($field)->select();
    }
}
